package edu.henu.personnel.service.notice;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.henu.personnel.constant.ErrorConstant;
import edu.henu.personnel.constant.PageHelperConstant;
import edu.henu.personnel.dao.NoticeDao;
import edu.henu.personnel.entity.Notice;
import edu.henu.personnel.exception.BusinessException;
import edu.henu.personnel.vo.NoticeVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @program: PersonnelProject
 * @description: 文章实现类
 * @author: zhuyang
 * @create: 2019-04-30 17:34
 */

@Transactional
@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    NoticeDao noticeDao;

    @Override
    public PageInfo<NoticeVO> getAllNotice(Integer pageNum) {
        PageHelper.startPage(pageNum, PageHelperConstant.afterNoticeSize);
        List<NoticeVO> allNotice = noticeDao.findAllNotice();
        PageInfo<NoticeVO> pageInfo = new PageInfo<>(allNotice);
        return pageInfo;
    }

    @Override
    public Notice getNoticeById(String nid) {
        if (null == nid)
            throw BusinessException.withErrorCode(ErrorConstant.Common.PARAM_IS_EMPTY);
        return noticeDao.findNoticeById(nid);
    }

    @Override
    public Notice getNoticeByTitle(String ntitle) {
        if (StringUtils.isBlank(ntitle)) {
            throw BusinessException.withErrorCode(ErrorConstant.Auth.USERNAME_PASSWORD_IS_EMPTY);
        }
        return noticeDao.findNoticeByTitle(ntitle);
    }

    @Override
    public PageInfo<NoticeVO> getNoticeListByCatelogid(Integer cid, int pageNum) {
        if (null == cid) {
            throw BusinessException.withErrorCode(ErrorConstant.Common.PARAM_IS_EMPTY);
        }
        PageHelper.startPage(pageNum, PageHelperConstant.displayNoticeSize);
        List<NoticeVO> noticeVOList = noticeDao.findNoticeByCatalogId(cid);
        PageInfo<NoticeVO> pageInfo = new PageInfo<>(noticeVOList);
        return pageInfo;
    }

    @Override
    public List<NoticeVO> getNoticeListToIndex(Integer cid) {
        if (null == cid) {
            throw BusinessException.withErrorCode(ErrorConstant.Common.PARAM_IS_EMPTY);
        }

        return noticeDao.findNoticeToIndex(cid);
    }

    @Override
    public int getCountOfNoticeInTheCatalog(List<Map<String, Object>> list) {
        return list.size();
    }

    @Override
    public int addNotice(Notice notice) {
        return noticeDao.insertNotice(notice);
    }

    @Override
    public int modifyNoticeInfo(Notice notice) {
        return noticeDao.updateNotice(notice);
    }

    @Override
    public int deleteNoticeInfo(String nid) {
        return noticeDao.deleteNotice(nid);
    }

    @Override
    public int addNoticeBrowse(String nid) {
        return noticeDao.addBrowses(nid);
    }


}
